<?php

namespace App\Services\admin;

use App\Models\admin\Application;
use App\Services\BaseService;
use App\Services\common\SysMapService;

/**
 * 应用 Service
 *
 * @Description:
 *
 * @Author: zhx
 *
 * @Date: 2023-09-06
 *
 * @LastEditTime: 2023-09-06
 */
class ApplicationService extends BaseService
{
    /**
     * 数据查询
     *
     * @return array
     */
    public static function getPageData($requestData)
    {
        [$perPage, $page, $orderBy, $orderDir] = [
            isset($requestData['perPage']) ? $requestData['perPage'] : 20,
            isset($requestData['page']) ? $requestData['page'] : 1,
            isset($requestData['orderBy']) ? $requestData['orderBy'] : 'id',
            isset($requestData['orderDir']) ? $requestData['orderDir'] : 'desc',
        ];
        $models = Application::query();
        if (isset($requestData['keywords'])) {
            $models->where('name', 'like', '%'.$requestData['keywords'].'%');
        }
        if (isset($requestData['name'])) {
            $models->where('name', 'like', '%'.$requestData['name'].'%');
        }
        if (isset($requestData['state'])) {
            $models->where('state', $requestData['state']);
        }
        $list = $models->orderBy($orderBy, $orderDir)->paginate($perPage);
        foreach ($list as $key => $item) {
            // 安装类别
            $icon = '';
            $dictionaryinfo = SysMapService::infoByCode('install_category');
            $dictionaryList = SysMapService::getData([
                'map_code' => explode(',', $item['install_category']),
                'parent_val' => $dictionaryinfo->id,
            ]);
            foreach ($dictionaryList as $value) {
                $icon .= "<img src='".$value->map_desc."' style='width: 20px; margin-right: 5px;' title='".$value->map_name."'>";
            }

            $list[$key]['icon_list'] = array_column($dictionaryList->toArray(), 'map_desc');
            $list[$key]['icon'] = $icon;
        }

        return $list;
    }

    /**
     * 数据查询
     *
     * @return array
     */
    public static function getData($requestData)
    {
        [$orderBy, $orderDir] = [
            isset($requestData['orderBy']) ? $requestData['orderBy'] : 'id',
            isset($requestData['orderDir']) ? $requestData['orderDir'] : 'desc',
        ];
        $models = Application::query();
        if (isset($requestData['keywords'])) {
            $models->where('name', 'like', '%'.$requestData['keywords'].'%');
        }
        if (isset($requestData['name'])) {
            $models->where('name', 'like', '%'.$requestData['name'].'%');
        }
        if (isset($requestData['state'])) {
            $models->where('state', $requestData['state']);
        }
        $list = $models->orderBy($orderBy, $orderDir)->get();

        foreach ($list as $key => $item) {
            // 安装类别
            $icon = '';
            $dictionaryinfo = SysMapService::infoByCode('install_category');
            $dictionaryList = SysMapService::getData([
                'map_code' => explode(',', $item['install_category']),
                'parent_val' => $dictionaryinfo->id,
            ]);
            foreach ($dictionaryList as $value) {
                $icon .= "<img src='".$value->map_desc."' style='width: 20px; margin-right: 5px;' title='".$value->map_name."'>";
            }

            $list[$key]['icon_list'] = array_column($dictionaryList->toArray(), 'map_desc');
            $list[$key]['icon'] = $icon;
        }

        return $list;
    }

    /**
     * 回收数据列表
     *
     * @Notes:
     *
     * @Author: zhx
     *
     * @Date: 2023-07-06
     *
     * @Time: 11:44
     *
     * @Interface getRecyclePageData
     */
    public static function getRecyclePageData($requestData)
    {
        $models = Application::onlyTrashed();

        if ($requestData['name']) {
            $models->where('name', $requestData['name']);
        }
        if ($requestData['installation_category']) {
            $models->where('installation_category', $requestData['installation_category']);
        }

        $list = $models->select('id', 'name', 'logo as image', 'remark', 'installation_category', 'module')->orderBy('id', 'desc')->paginate();

        foreach ($list as $key => $value) {
            // 安装类别【1.微信小程序|2.微信公众号|3.PC|4.APP】
            $installation_category = '';
            if ($value['installation_category'] == 1) {
                $installation_category = '<button type="button" class="tag-item layui-btn layui-btn-primary layui-btn-sm layui-btn-radius" onclick="">微信小程序</button>';
            } elseif ($value['installation_category'] == 2) {
                $installation_category = '<button type="button" class="tag-item layui-btn layui-btn-primary layui-btn-sm layui-btn-radius">微信公众号</button>';
            } elseif ($value['installation_category'] == 3) {
                $installation_category = '<button type="button" class="tag-item layui-btn layui-btn-primary layui-btn-sm layui-btn-radius tag-item-warm">&nbsp;PC&nbsp;</button>';
            } elseif ($value['installation_category'] == 4) {
                $installation_category = '<button type="button" class="tag-item layui-btn layui-btn-primary layui-btn-sm layui-btn-radius tag-item-warm" style="background-color: #f6ad55;">APP</button>';
            }

            $list[$key]['title'] = $value['name'].'&nbsp;&nbsp;'.$installation_category;
            $list[$key]['time'] = '永久';
        }

        return $list;
    }

    /**
     * 编辑
     *
     * @return \Illuminate\Http\JsonResponse
     * User: zhx
     * Date: 2023/6/30 14:28
     */
    public static function edit($id, $requestData)
    {
        $models = Application::query();
        $ret = $models->where('id', $id)->update($requestData);

        return $ret;
    }

    /**
     * 添加
     *
     * @Notes:
     *
     * @Author: zhx
     *
     * @Date: 2023-07-01
     *
     * @Time: 14:02
     *
     * @Interface create
     */
    public static function create($requestData)
    {
        $models = Application::query();
        $ret = $models->create($requestData);

        return $ret;
    }

    /**
     * 删除
     *
     * @return \Illuminate\Http\JsonResponse
     * User: zhx
     * Date: 2023/6/30 14:30
     */
    public static function destroy($id)
    {
        $models = Application::query();
        $ret = $models->where('id', $id)->delete();

        return $ret;
    }

    /**
     * 详细
     *
     * @Notes:
     *
     * @Author: zhx
     *
     * @Date: 2023-07-03
     *
     * @Time: 16:14
     *
     * @Interface info
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public static function getInfo($id)
    {
        $models = Application::query();
        $info = $models->where(['id' => $id])->first();

        return $info;
    }

    /**
     * 详细
     *
     * @Notes:
     *
     * @Author: zhx
     *
     * @Date: 2023-07-03
     *
     * @Time: 16:14
     *
     * @Interface info
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public static function getInfoByModule($module)
    {
        $models = Application::query();
        $info = $models->where(['module', $module])->first();

        return $info;
    }

    /**
     * 恢复应用
     *
     * @Notes:
     *
     * @Author: zhx
     *
     * @Date: 2023-07-06
     *
     * @Time: 12:00
     *
     * @Interface setDestroy
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public static function setDestroy($id)
    {
        $models = Application::onlyTrashed();

        //----------------------------------------------------
        // 操作
        //----------------------------------------------------
        $ret = $models->where('id', $id)->restore();

        return $ret;
    }

    /**
     * 删除
     *
     * @return \Illuminate\Http\JsonResponse
     * User: zhx
     * Date: 2023/6/30 14:30
     */
    public static function delete($id)
    {
        $models = Application::onlyTrashed();
        $ret = $models->where('id', $id)->forceDelete();

        return $ret;
    }
}
